<?php

class UserController  extends AuthController {
	public $DB;
	public function __Init(){
		$this->DB = new UserModel;
	}

	public function userList(){
		$userData = $this->DB->getUserList();
		$this->assign('user',$userData);
		$this->display('userlist.html');
	}
	public function editPass(){
		if(ISPOST){
			$info = $this->DB->where("aid = {$_SESSION['aid']}")->find();
			$oldPass = md5($_POST['oldpass']);
			// p($info);exit;
			if($info && $info['password'] == $oldPass){
				// p($info);
				// p($info['password']);
				if($info['password'] == $oldPass){
					// p($info['password']);
					unset($_POST['oldpass']);
					if($this->DB->rePassword()){
						session_unset();
						session_destroy();
						$this->success('修改密码成功请使用新的密码',__MODEL__.'&c=Index&a=rightIndex');
						return true;
					}else{
						$this->error($this->DB->error,__MODEL__);
						return false;
					}
				}
			}else{
				$this->error('原密码错误!',__MODEL__.'&c=User&a=editPass');
				return false;
			}
			
		}else{
			$this->display('repass.html');
		}
		
	}
	public function deleteUser(){
		$uid = $_GET['aid'];
		if(empty($uid)){
			$this->error('非法操作',__CONTROLLER__);
		}
		if($this->DB->deleteUser($uid)){
			$this->success('删除用户成功',__CONTROLLER__);
			return true;
		}else{
			$this->error($this->DB->error);
			return false;
		}
	}

	public function addUser(){
		if(ISPOST){
			if(!empty($_POST['username'])){
				if(!empty($_POST['password'])){
					$_POST['password'] = md5($_POST['password']);
					if($this->DB->addUser()){
						$this->success('添加用户成功');
						return true;
					}else{
						$this->error($this->DB->error);
						return false;
					}
				}else{
					$this->error('密码不能为空');
				}
			}else{
				$this->error('用户名不能为空');
			}
			
		}else{
			$this->display('addUser.html');
		}
	}
}


?>